package com.haixiaoke.smartLock.tianwanghulian;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.domain.*;
import com.haixiaoke.mapper.SmartLockFingerprintMapper;
import com.haixiaoke.service.*;
import com.haixiaoke.vo.TwhlDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 天网互联-日志回调处理
 *
 * @author qixi
 * @date 2023-05-27
 */
@Service
public class SmartLockTwhlLogEventHandleService {

    @Autowired
    private ILockWarningLogService lockWarningLogService;
    @Autowired
    private ILockCommandLogService lockCommandLogService;
    @Autowired
    private SmartLockFingerprintMapper smartLockFingerprintMapper;
    @Autowired
    private ILockUnlockLogService lockUnlockLogService;
    @Autowired
    private ILockOfflineLogService lockOfflineLogService;
    @Autowired
    private ILockInfoLogService lockInfoLogService;

    public void smartLockWarningLog(TwhlDataVo data) {
        Date date = new Date();

        LockWarningLog log = new LockWarningLog();
        log.setLockSn(data.getDeviceNum());
        log.setWarningType(data.getAlarmType());
        log.setCreateTime(date);
        log.setBrandId(LockWarningLog.TWHL_BRAND_ID);
        log.setBrandName(LockWarningLog.TWHL_BRAND_NAME);
        log.setModelId(LockWarningLog.TWHL_BRAND_MODEL_ID);
        log.setModelName(LockWarningLog.TWHL_BRAND_MODEL_NAME);
        lockWarningLogService.insertLockWarningLog(log);
    }


    @Transactional
    public void smartLockCommandLog(TwhlDataVo data) {
        Date date = new Date();

        LockCommandLog log = new LockCommandLog();
        log.setCommandType(data.getCommandType());
        log.setLockSn(data.getDeviceNum());
        log.setCallbackState(data.getCallbackState());
        log.setRemark(data.getUuid());
        log.setCreateTime(date);
        log.setBrandId(LockWarningLog.TWHL_BRAND_ID);
        log.setBrandName(LockWarningLog.TWHL_BRAND_NAME);
        log.setModelId(LockWarningLog.TWHL_BRAND_MODEL_ID);
        log.setModelName(LockWarningLog.TWHL_BRAND_MODEL_NAME);
        lockCommandLogService.insertLockCommandLog(log);

        // 指纹下发指令回调，修改指纹下方状态
        if (data.getCommandType() == 6) {
            SmartLockFingerprint smartLockFingerprint = new SmartLockFingerprint();
            smartLockFingerprint.setSendStatus(SmartLockFingerprint.LOCK_STATE_SEND_PROCESS);
            smartLockFingerprint.setSn(data.getDeviceNum());
            List<SmartLockFingerprint> smartLockFingerprints = smartLockFingerprintMapper.selectSmartLockFingerprintList(smartLockFingerprint);
            if (ObjectUtil.isNotNull(smartLockFingerprints)) {
                for (SmartLockFingerprint lockFingerprint : smartLockFingerprints) {
                    lockFingerprint.setSendStatus(SmartLockFingerprint.LOCK_STATE_SEND_FINISH);
                    smartLockFingerprintMapper.updateSmartLockFingerprint(lockFingerprint);
                }
            }
        }
    }

    public void smartLockUnlockLog(TwhlDataVo data) {
        Date date = new Date();

        LockUnlockLog log = new LockUnlockLog();
        log.setLockSn(data.getDeviceNum());
        Integer unlockType = data.getUnlockType();
        log.setUnlockType(unlockType);
        String status = data.getStatus();
        if (StrUtil.isNotEmpty(status)) {
            Integer unlockState = Convert.toInt(status);
            log.setUnlockState(unlockState);
        }
        String time = data.getUnlockTime();
        if (StrUtil.isNotEmpty(status)) {
            Date unlockTime = Convert.toDate(time);
            log.setUnlockTime(unlockTime);
        }
        log.setIdentification(data.getIdentification());
        log.setBatteryInfo(data.getBatteryInfo());

        // 关锁类型设置
        if (LockUnlockLog.UNLOCK_TYPE_INSIDE_UNLOCK.equals(unlockType)) {
            if (1 == data.getRecordType()) {
                log.setRecordType(LockUnlockLog.RECORD_TYPE_INSIDE_UNLOCK);
            } else if (2 == data.getRecordType()) {
                log.setRecordType(LockUnlockLog.RECORD_TYPE_BACK_LOCK);
            }
        } else if (LockUnlockLog.UNLOCK_TYPE_BACK_LOCK.equals(unlockType)){
            log.setRecordType(data.getRecordType());
        }
        log.setLockState(LockUnlockLog.LOCK_STATE_ONLINE);
        log.setBrandId(LockWarningLog.TWHL_BRAND_ID);
        log.setBrandName(LockWarningLog.TWHL_BRAND_NAME);
        log.setModelId(LockWarningLog.TWHL_BRAND_MODEL_ID);
        log.setModelName(LockWarningLog.TWHL_BRAND_MODEL_NAME);
        log.setCreateTime(date);
        lockUnlockLogService.insertLockUnlockLog(log);
    }

    public void smartLockOfflineUnlockLog(List<Map<String, Object>> list) {
        Date date = new Date();
        for (Map<String, Object> map : list) {
            TwhlDataVo data = mapToTwhlDataVoConvert(map);
            LockUnlockLog log = new LockUnlockLog();
            log.setLockSn(data.getDeviceNum());
            Integer unlockType = data.getUnlockType();
            log.setUnlockType(unlockType);
            String status = data.getStatus();
            if (StrUtil.isNotEmpty(status)) {
                Integer unlockState = Convert.toInt(status);
                log.setUnlockState(unlockState);
            }
            // 时间戳处理
            String time = data.getUnlockTime();
            if (StrUtil.isNotEmpty(time)) {
                Date unlockTime = new Date(Convert.toLong(time));
                unlockTime = DateUtil.parse(DateUtil.format(unlockTime, "yyyy-MM-dd HH:mm:ss"));
                log.setUnlockTime(unlockTime);
            }
            log.setIdentification(data.getIdentification());
            log.setBatteryInfo(data.getBatteryInfo());

            // 关锁类型设置
            // 接口文档 反锁(1:关闭反锁 2:打开反锁) 门内开锁(1:门内开门 2:弹出方舌) 其它开锁类型这个字段无效 -> 入库 关锁类型1:关闭反锁 2:打开反锁3:门内开门 4:弹出方舌
            if (LockUnlockLog.UNLOCK_TYPE_INSIDE_UNLOCK.equals(unlockType)) {
                if (1 == data.getRecordType()) {
                    log.setRecordType(LockUnlockLog.RECORD_TYPE_INSIDE_UNLOCK);
                } else if (2 == data.getRecordType()) {
                    log.setRecordType(LockUnlockLog.RECORD_TYPE_BACK_LOCK);
                }
            } else {
                log.setRecordType(data.getRecordType());
            }
            log.setLockState(LockUnlockLog.LOCK_STATE_OFFLINE);
            log.setBrandId(LockWarningLog.TWHL_BRAND_ID);
            log.setBrandName(LockWarningLog.TWHL_BRAND_NAME);
            log.setModelId(LockWarningLog.TWHL_BRAND_MODEL_ID);
            log.setModelName(LockWarningLog.TWHL_BRAND_MODEL_NAME);
            log.setCreateTime(date);
            lockUnlockLogService.insertLockUnlockLog(log);
        }
    }

    public void smartLockOfflineLog(TwhlDataVo data) {
        Date date = new Date();

        LockOfflineLog log = new LockOfflineLog();
        log.setLockSn(data.getDeviceNum());
        log.setCreateTime(date);
        String dateTime = data.getDateTime();
        if (StrUtil.isNotEmpty(dateTime)) {
            log.setOfflineTime(Convert.toDate(dateTime));
        }

        log.setBrandId(LockWarningLog.TWHL_BRAND_ID);
        log.setBrandName(LockWarningLog.TWHL_BRAND_NAME);
        log.setModelId(LockWarningLog.TWHL_BRAND_MODEL_ID);
        log.setModelName(LockWarningLog.TWHL_BRAND_MODEL_NAME);
        lockOfflineLogService.insertLockOfflineLog(log);
    }

    public void smartLockInfoLog(TwhlDataVo data) {
        Date date = new Date();

        LockInfoLog log = new LockInfoLog();
        log.setSignalStrength(data.getSignalStrength());
        log.setBattery(data.getBattery());
        log.setInfoType(data.getDataType());
        log.setGateState(data.getGateState());
        log.setLockSn(data.getDeviceNum());
        log.setCreateTime(date);
        log.setBrandId(LockWarningLog.TWHL_BRAND_ID);
        log.setBrandName(LockWarningLog.TWHL_BRAND_NAME);
        log.setModelId(LockWarningLog.TWHL_BRAND_MODEL_ID);
        log.setModelName(LockWarningLog.TWHL_BRAND_MODEL_NAME);
        lockInfoLogService.insertLockInfoLog(log);

    }


    private TwhlDataVo mapToTwhlDataVoConvert(Map<String, Object> map) {
        TwhlDataVo twhlDataVo = new TwhlDataVo();
        twhlDataVo.setDeviceNum(Convert.toStr(map.get("deviceNum")));
        twhlDataVo.setBattery(Convert.toInt(map.get("battery")));
        twhlDataVo.setSignalStrength(Convert.toInt(map.get("signalStrength")));
        twhlDataVo.setDataType(Convert.toInt(map.get("dataType")));
        twhlDataVo.setAlarmType(Convert.toInt(map.get("alarmType")));
        twhlDataVo.setUuid(Convert.toStr(map.get("uuid")));
        twhlDataVo.setCommandType(Convert.toInt(map.get("commandType")));
        twhlDataVo.setCallbackState(Convert.toInt(map.get("callbackState")));
        twhlDataVo.setUnlockType(Convert.toInt(map.get("unlockType")));
        twhlDataVo.setIdentification(Convert.toStr(map.get("identification")));
        twhlDataVo.setRecordType(Convert.toInt(map.get("recordType")));
        twhlDataVo.setUnlockTime(Convert.toStr(map.get("unlockTime")));
        twhlDataVo.setBatteryInfo(Convert.toStr(map.get("batteryInfo")));
        twhlDataVo.setStatus(Convert.toStr(map.get("status")));
        twhlDataVo.setDateTime(Convert.toStr(map.get("dateTime")));
        twhlDataVo.setGateState(Convert.toInt(map.get("gateState")));
        return twhlDataVo;
    }


}
